package tech.aistar.corejava.day14.exercise;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by 豆梦寒 on 2019/8/2.
 */
public class ProductDaoImpl implements IProductDao {

    public static final String PATH ="src/tech/aistar/corejava/day14/exercise/product.txt";


    @Override

    public List<Product> findAll() {
        //构建读取对象类型的文件的字节输入流
        ObjectInputStream in = null;

        List<Product> products = null;

        try {
            in = new ObjectInputStream(new FileInputStream(PATH));
            products = (List<Product>)in.readObject();

        } catch (IOException e) {
            return null;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
        return products;
    }

    @Override
    public void save(Product product) {
        ObjectOutputStream out = null;

        //先读取
        List<Product> products = findAll();


        try {
            //如果文件不存在  创建了一个文件 永远重新初始化文件
            out = new ObjectOutputStream(new FileOutputStream(PATH));
            //参数有效性判断
//            if (null != products) {
//                //将新加入的product对象添加到这个集合中
//                products.add(product);
//            }else {
//                //重新初始化
//                products = new ArrayList<>();
//                products.add(product);
//            }
            if(null == products)
                products = new ArrayList<>();

            products.add(product);

            //写入...
            out.writeObject(products);


        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(null!=out){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void saveList(List<Product> products) {

    }

    @Override
    public void delByname(String name) {
        List<Product> products = findAll();

        if (products != null) {
            Iterator<Product> iterator = products.iterator();
            while (iterator.hasNext()){
                Product p = iterator.next();
                if (p.getName().equals(name)) {
                    iterator.remove();
                }
            }

            ObjectOutputStream out = null;

            try {
                out = new ObjectOutputStream(new FileOutputStream(PATH));

                for (Product p:products){
                    save(p);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (null != out) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }

    }
}
